package org.jeecgframework.web.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.service.UploadContactsService;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.pojo.base.ZyCinemaEntity;
import org.jeecgframework.web.system.pojo.base.ZyContactsEntity;
import org.jeecgframework.web.util.ExcelUtil;
import org.jeecgframework.web.util.StaticValue;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

/**   
 * @Title: UploadContactsServiceImpl
 * @Description: 上传联系人信息
 * @author ZhangYunlong
 * @date 2016-04-01
 * @version V1.0   
 *
 */
@Service("uploadContactsService")
@Transactional
public class UploadContactsServiceImpl extends CommonServiceImpl implements
		UploadContactsService {

	//联系业务
	public final static String LINK_BUSINESS = "联系业务";
	public final static String LINK_BUSINESS_INTALL = "安装";
	public final static String LINK_BUSINESS_INSPECTION = "巡检";
	public final static String LINK_BUSINESS_REPAIR= "维修";
	public final static String LINK_BUSINESS_SHIFT = "移机";
	public final static String LINK_BUSINESS_REFORM = "改造";
	//职位
	public final static String CONTACT_POSITION_C_MANAGER = "影院经理"; 
	public final static String CONTACT_POSITION_C_TECHNICAL_DIRECTOR = "影院技术主管"; 
	public final static String CONTACT_POSITION_C_TECHNICIAN = "影院技术员"; 
	public final static String CONTACT_POSITION_TP_SERVICE_MANAGER = "第三方服务商经理"; 
	public final static String CONTACT_POSITION_TP_SERVER_BUSINESSR = "第三方服务商商务"; 
	public final static String CONTACT_POSITION_TP_ENGINEER = "第三方工程师"; 
	public final static String CONTACT_POSITION_TP_AREA_ENGINEER = "第三方区域工程师"; 
	public final static String CONTACT_POSITION_BARCO_GENERAL_MANAGER = "可总经理"; 
	public final static String CONTACT_POSITION_BARCO_VICE_GENERAL_MANAGER = "副总经理"; 
	public final static String CONTACT_POSITION_DIRECTOR = "总监"; 
	public final static String CONTACT_POSITION_DEPARTMENT_MANAGER = "部门经理"; 
	public final static String CONTACT_POSITION_ADVISER = "顾问"; 
	public final static String CONTACT_POSITION_EMPLOYEE = "职员"; 
	
	/**
	 * @Description 解析EXCEL
	 * @param HttpServletRequest 通过前台请求获得文件流
	 */
	public List<ZyContactsEntity> parseExcel(HttpServletRequest req) {
		List<ZyContactsEntity> excelList = new ArrayList<ZyContactsEntity>();
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) req;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		XSSFWorkbook hwk = null;
		try {
			for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
				MultipartFile file = entity.getValue();// 获取上传文件对象
				hwk = ExcelUtil.analyzeExcel2(file.getInputStream());
				excelList = parseExcelToList(hwk);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return excelList;
	}

	/**
	 * @Description 将通过POI解析出来的EXCEL转换成对应的实体LIST
	 * @param XSSFWorkbook Excel2007+对象
	 */
	private List<ZyContactsEntity> parseExcelToList(XSSFWorkbook hwb) {
		System.out.println("开始解析EXCEL ----");
		System.out.println("走了没有");
		List<ZyContactsEntity> result = new ArrayList<ZyContactsEntity>();
		// 得到第一个工作表
		XSSFSheet sheet = hwb.getSheetAt(0);
		XSSFRow row = null;
		String position = "";
		ZyContactsEntity zyContactsEntity;
		ZyCinemaEntity belongCinema = null;
		TSDepart belongOrg = null;
		for (int j = 1; j < sheet.getPhysicalNumberOfRows(); j++) {
			row = sheet.getRow(j);
			zyContactsEntity = new ZyContactsEntity();
			belongCinema = findUniqueByProperty(ZyCinemaEntity.class, "name", ExcelUtil.getCellValue(row.getCell(0)));
			belongOrg = findUniqueByProperty(TSDepart.class, "departname", ExcelUtil.getCellValue(row.getCell(0)));
			zyContactsEntity.setBelongCinema(belongCinema);
			zyContactsEntity.setBelongOrg(belongOrg);
			zyContactsEntity.setName(ExcelUtil.getCellValue(row.getCell(1)));
			zyContactsEntity.setTelephone(ExcelUtil.getCellValue(row.getCell(2)));
			zyContactsEntity.setCellphone1(ExcelUtil.getCellValue(row.getCell(3)));
			zyContactsEntity.setCellphone2(ExcelUtil.getCellValue(row.getCell(4)));
			zyContactsEntity.setGender(ExcelUtil.getCellValue(row.getCell(5)).trim().equals("男") ? true : false);
			zyContactsEntity.setState(ExcelUtil.getCellValue(row.getCell(6)).trim().equals("在职") ? StaticValue.DIC_ENGINEER_STATE_ONJOB:StaticValue.DIC_ENGINEER_STATE_QUIT);
			zyContactsEntity.setEmail(ExcelUtil.getCellValue(row.getCell(7)));
			switch(ExcelUtil.getCellValue(row.getCell(8)).trim()){
				case CONTACT_POSITION_C_MANAGER : 
					position = StaticValue.DIC_CONTACT_POSITION_C_MANAGER;
					break;
				case CONTACT_POSITION_C_TECHNICAL_DIRECTOR : 
					position = StaticValue.DIC_CONTACT_POSITION_C_TECHNICAL_DIRECTOR;
					break;
				case CONTACT_POSITION_C_TECHNICIAN : 
					position = StaticValue.DIC_CONTACT_POSITION_C_TECHNICIAN;
					break;
				case CONTACT_POSITION_TP_SERVICE_MANAGER : 
					position = StaticValue.DIC_CONTACT_POSITION_TP_SERVICE_MANAGER;
					break;
				case CONTACT_POSITION_TP_SERVER_BUSINESSR : 
					position = StaticValue.DIC_CONTACT_POSITION_TP_SERVER_BUSINESSR;
					break;
				case CONTACT_POSITION_TP_ENGINEER : 
					position = StaticValue.DIC_CONTACT_POSITION_TP_ENGINEER;
					break;
				case CONTACT_POSITION_TP_AREA_ENGINEER : 
					position = StaticValue.DIC_CONTACT_POSITION_TP_AREA_ENGINEER;
					break;
				case CONTACT_POSITION_BARCO_GENERAL_MANAGER : 
					position = StaticValue.DIC_CONTACT_POSITION_BARCO_GENERAL_MANAGER;
					break;
				case CONTACT_POSITION_BARCO_VICE_GENERAL_MANAGER : 
					position = StaticValue.DIC_CONTACT_POSITION_BARCO_VICE_GENERAL_MANAGER;
					break;
				case CONTACT_POSITION_DIRECTOR : 
					position = StaticValue.DIC_CONTACT_POSITION_DIRECTOR;
					break;
				case CONTACT_POSITION_DEPARTMENT_MANAGER : 
					position = StaticValue.DIC_CONTACT_POSITION_DEPARTMENT_MANAGER;
					break;
				case CONTACT_POSITION_ADVISER : 
					position = StaticValue.DIC_CONTACT_POSITION_ADVISER;
					break;
				case CONTACT_POSITION_EMPLOYEE : 
					position = StaticValue.DIC_CONTACT_POSITION_EMPLOYEE;
					break;
				default:
					position = "";
			}
			zyContactsEntity.setPosition(position);
//				zyContactsEntity.setAuthentication(ExcelUtil.getCellValue(row.getCell(9)));
			zyContactsEntity.setSource(ExcelUtil.getCellValue(row.getCell(9)));
			zyContactsEntity.setType(ExcelUtil.getCellValue(row.getCell(10)).trim().equals("影院联系人") ? StaticValue.DIC_CONTACT_TYPE_CINEMA_CONTACT : StaticValue.DIC_CONTACT_TYPE_TP_CONTACT);
			zyContactsEntity.setConBusiness(splitConBusiness(ExcelUtil.getCellValue(row.getCell(11))));
			zyContactsEntity.setIsDelete("0");
			result.add(zyContactsEntity);
		}
		System.out.println("-----解析完毕----");
		return result;
	}

	private String splitConBusiness(String conBusiness){
		StringBuilder result = new StringBuilder();
		String mark="、";
		String[] temp = {};
		if(StringUtil.isNotEmpty(conBusiness) && conBusiness.contains(mark)){
			temp = conBusiness.split(mark);
		}else{
			temp = new String[1];
			temp[0] = conBusiness;
		}
		for(int i = 0;i < temp.length ;i++){
			switch(temp[i].trim()){
				case LINK_BUSINESS : 
					result.append(StaticValue.DIC_LINK_BUSINESS);
					break;
				case LINK_BUSINESS_INTALL : 
					result.append(StaticValue.DIC_LINK_BUSINESS_INTALL);
					break;
				case LINK_BUSINESS_INSPECTION : 
					result.append(StaticValue.DIC_LINK_BUSINESS_INSPECTION);
					break;
				case LINK_BUSINESS_REPAIR : 
					result.append(StaticValue.DIC_LINK_BUSINESS_REPAIR);
					break;
				case LINK_BUSINESS_SHIFT : 
					result.append(StaticValue.DIC_LINK_BUSINESS_SHIFT);
					break;
				case LINK_BUSINESS_REFORM : 
					result.append(StaticValue.DIC_LINK_BUSINESS_REFORM);
					break;
				default:
					result.append("");
			}
			if(i < temp.length-1){
				result.append(",");
			}
		}
		return result.toString();
	}
	
	/**
	 * @Description 批量保存
	 * @param List<T>
	 */
	public <T> void batchSave(List<T> entitys) {
		for (T t : entitys) {
			super.save(t);
		}
	}
}
